/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.tutwiz.backingBeans;

import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane.TitlePaneLayout;
import javax.validation.constraints.Size;
import net.tutwiz.bindingBeans.TutorialBindingBean;
import net.tutwiz.model.Step;
import net.tutwiz.model.Tutorial;
import net.tutwiz.persistence.TutorialPersistenceManager;

/**
 * Backs up the view of editTutorial.xhtml
 * 
 * @author vind
 */
@ManagedBean(name = "editTutorialBB")
@ViewScoped
public class EditTutorialBackingBean {

    //These properties are only for setting validation constraints
    @Size(min = 10, message = "The title should be clear and descriptive, please use at least 10 characters.")
    private String title;
    @Size(min = 10, message = "Give a good description of what the tutorial covers, please use at least 10 characters.")
    private String description;
    
    private TutorialPersistenceManager pm = new TutorialPersistenceManager();
    private Tutorial tutorial;

    public EditTutorialBackingBean() {
        Long id = null;

        try {
            id = Long.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("tutorial"));
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        if (id == null) {
            tutorial = new Tutorial("Your tutorial title", "Your tutorial description");
        } else {
            tutorial = pm.get(id);
        }
    }

    public String getTitle() {
        return tutorial.getTitle();
    }

    public void setTitle(String title) {
        tutorial.setTitle(title);
    }

    public String getDescription() {
        return tutorial.getDescription();
    }

    public void setDescription(String desc) {
        tutorial.setDescription(desc);
    }
    
    public List<Step> getSteps() {
        return tutorial.getSteps();
    }
    
    public Tutorial getTutorial() {
        return tutorial;
    }
    
    public void saveTutorial() {
        if (tutorial.getId() == null) {
            pm.save(tutorial);
        } else {
            pm.update(tutorial);
        }
    }

    public void deleteTutorial(Long id) {
        pm.delete(pm.get(id));
    }
}
